#include "drawchart.h"
#include "nightcharts.h"

DrawChart::DrawChart()
{
}

void DrawChart::drawLineChart(QString title, QString line_name)
{
    QString id("white");
    QPixmap pixmap(700,700);
    pixmap.fill(QColor(id).rgba());

    // write the color name
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);


    QPen pen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    QPen pen1(Qt::black, 3, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
    QPen pen2(Qt::black, 1, Qt::DashDotLine, Qt::SquareCap, Qt::MiterJoin);

    // draw rectangle for chart
    painter.setPen(pen1);
    painter.fillRect(50,50,600,500,Qt::lightGray);
    painter.drawRect(50,50,600,500);

    // draw short lines of x
    painter.setPen(pen2);
    painter.drawLine(100,550,100,540);
    painter.drawLine(150,550,150,540);
    painter.drawLine(200,550,200,540);
    painter.drawLine(250,550,250,540);
    painter.drawLine(300,550,300,540);
    painter.drawLine(350,550,350,540);
    painter.drawLine(400,550,400,540);
    painter.drawLine(450,550,450,540);
    painter.drawLine(500,550,500,540);
    painter.drawLine(550,550,550,540);
    painter.drawLine(600,550,600,540);
    painter.drawLine(650,550,650,540);

    // draw lines of y
    painter.drawLine(50,450,650,450);
    painter.drawLine(50,350,650,350);
    painter.drawLine(50,250,650,250);
    painter.drawLine(50,150,650,150);

    // description for red line
    QPen pen3(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter.setPen(pen);
    painter.drawLine(50,650,70,650);
    QFont font;
    font.setPixelSize(20);
    painter.setPen(pen3);
    painter.setFont(font);
    painter.drawText(80,650 + 8,line_name);
    painter.drawText(100, 30, title);

    // draw red line
//    qreal min = this->findMin() ;
//    qreal max = this->findMax() ;
    qreal gap = this->findMax() - this->findMin() ;
    gap = gap > 50.0 ? gap : 50.0 ;
//    int Counter=0;
//    while(Counter < (xList.size()-1))
//    {
//        painter.setPen(pen);
//        painter.drawLine(xList.value(Counter)*25+50,550-yDrawList.value(Counter)/(gap)*400,
//                         xList.value(Counter+1)*25+50,550-yDrawList.value(Counter+1)/(gap)*400);
//        Counter++;
//    }
    painter.setPen(pen);
    switch (line_y.size())
    {
    case 0:
    case 1:
        break;
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
        for (int i=0; i<line_y.size()-1; i++)
        {
//            painter.setPen(pen);
            painter.drawLine(i*50+50, 550 - line_y.at(i)/(gap)*400,
                             (i+1)*50+50, 550 - line_y.at(i+1)/(gap)*400);
        }
        break;
    case 12:
    case 13:
        for (int i=0; i<line_y.size()-1; i++)
        {
//            painter.setPen(pen);
            painter.drawLine((i)*25+50, 550 - line_y.at(i)/(gap)*400,
                             (i+1)*25+50, 550 - line_y.at(i+1)/(gap)*400);
        }
        break;
    case 30:
    case 31:
        for (int i=0; i<line_y.size()-1; i++)
        {
//            painter.setPen(pen);
            painter.drawLine((i)*10+50, 550 - line_y.at(i)/(gap)*400,
                             (i+1)*10+50, 550 - line_y.at(i+1)/(gap)*400);
        }
        break;
    default:
        for (int i=0; i<line_y.size()-1; i++)
        {
//            painter.setPen(pen);
            painter.drawLine((i)*25+50, 550 - line_y.at(i)/(gap)*400,
                             (i+1)*25+50, 550 - line_y.at(i+1)/(gap)*400);
        }
        break;
    }

    pixmap.save(QDir::homePath() + "/chart.png", "PNG");
}

void DrawChart::drawPieChart(QString title)
{
    QString id("white");
    QPixmap pixmap(700,900);
    pixmap.fill(QColor(id).rgba());

    // write the color name
    QFont font;
    font.setPixelSize(24);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setFont(font);
    painter.drawText(100, 30, title);
    Nightcharts PieChart;
    PieChart.setType(Nightcharts::Dpie);//{Histogramm , Pie, Dpie};
    PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
    PieChart.setCords(50,50,600,500);
//    PieChart.addPiece("Item1",QColor(200,10,50),34);
//    PieChart.addPiece("Item2",Qt::green,27);
//    PieChart.addPiece("Item3",Qt::cyan,14);
//    PieChart.addPiece("Item4",Qt::yellow,7);
//    PieChart.addPiece("Item5",Qt::blue,4);
    for (int i=0; i<pie_name.size(); i++)
    {
        PieChart.addPiece(pie_name.at(i),pie_color.at(i),pie_percent.at(i));
    }
    PieChart.draw(&painter);
    PieChart.drawLegend(&painter);
    pixmap.save(QDir::homePath() + "/chart.png", "PNG");
}

void DrawChart::inputLineChart(int yNum)
{
    line_y.append(yNum);
}

void DrawChart::inputPieChart(QString name, Qt::GlobalColor color, float percentage)
{
    pie_name.append(name);
    pie_color.append(color);
    pie_percent.append(percentage);
}

void DrawChart::inputClear()
{
    line_y.clear();
    pie_name.clear();
    pie_color.clear();
    pie_percent.clear();
}

void DrawChart::saveChart()
{
}

qreal DrawChart::findMax()
{
    QList<qreal> yList = this->line_y;
    qreal max = yList.at(0);
    for (int i=1; i<yList.size(); i++)
    {
        if (yList.at(i) > max)
        {
            max = yList.at(i);
        }
    }
    return max;
}

qreal DrawChart::findMin()
{
    QList<qreal> yList = this->line_y;
    qreal min = yList.at(0);
    for (int i=1; i<yList.size(); i++)
    {
        if (yList.at(i) < min)
        {
            min = yList.at(i);
        }
    }
    return min;
}
